feat(events): add WebSocketConnectionContext helper for APIGatewayV2WebSocketEvent (#273)#606
Conversation
8cc24e7 to
e2f1a0d
Compare
|
Hello @ramanathan1504, thanks for your contribution, any help is appreciated ❤️ Before diving into implementation details, I'd love to understand the use case better and make sure we're solving the right problem. Every class in Issue #273 asks for a Were you thinking about Lambda Managed Instances and their multi-concurrency model as a use case here? Besides that, the example provided in the README looks to me functionally identical to the one using using WebSocketConnectionContext WebSocketConnectionContext connection = event.getConnectionContext();
if (connection != null) {
String connectionId = connection.getConnectionId();
String endpoint = connection.getManagementApiEndpoint();
}using RequestContext var rc = event.getRequestContext();
if (rc != null && rc.getConnectionId() != null) {
String connectionId = rc.getConnectionId();
String endpoint = "https://" + rc.getDomainName() + "/" + rc.getStage();
}The only saving is the URL concatenation. Could you share a scenario where this meaningfully reduces the complexity for the end user? Thanks again for your time! |
|
Thanks for the detailed feedback. My intention was not to model a persistent runtime-managed WebSocket session like Spring/Jakarta EE, but to provide a lightweight convenience abstraction around commonly grouped connection metadata already present in the payload. In many Lambda WebSocket handlers, developers repeatedly reconstruct:
This logic often gets duplicated across handlers/services and can become error-prone. That said, I understand the concern about keeping aws-lambda-java-events strictly schema-aligned. An alternative could be:
Happy to adapt the PR direction based on what best fits the library design principles. |
… APIGatewayV2WebSocketEvent
e4a52ae to
2ecc03f
Compare
|
Hey @ramanathan1504, Thanks for the answer. Reading through your comment I'm still not convinced that this change should be included in the events library for the following reasons:
More broadly, adding any derived/computed method to an event class would be a first for this library. Even a slim If there are no other arguments I'm leaning to close the PR. I will also deal with issue #273 because I'm convinced from the writing that the author was asking about managed persistent sessions, which aren't possible in Lambda OD. I really appreciate your effort ❤️. If you find anything else you want to contribute to (we have a very long backlog and we welcome any external contribution) you can open a new issue or pick one of the existent ones. Just add some comments on what you want to do so I can give you advice on how to best deal with that. |
|
Thanks for the thorough explanation — the precedent concern makes sense and I agree it’s a reasonable place to draw the line. Happy for you to close this one. I’ll keep an eye on the backlog for something that fits the library’s design better. Appreciate the guidance throughout! |
This change addresses #273 by adding a session-like, Lambda-safe connection context for API Gateway WebSocket events.
What changed
WebSocketConnectionContexttoAPIGatewayV2WebSocketEventinaws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/APIGatewayV2WebSocketEvent.javaAPIGatewayV2WebSocketEvent#getConnectionContext()APIGatewayV2WebSocketEvent.RequestContext#getConnectionContext()getManagementApiEndpoint()to buildhttps://{domainName}/{stage}for API Gateway Management API usagedomainName,stage)aws-lambda-java-events/src/test/java/com/amazonaws/services/lambda/runtime/events/APIGatewayV2WebSocketEventTest.javaaws-lambda-java-events/README.mdaws-lambda-java-events/RELEASE.CHANGELOG.mdWhy
AWS Lambda WebSocket events do not provide a native Java
WebSocketSession. This helper provides a lightweight session-like object built from event metadata (connectionId,domainName,stage) so handlers can easily pass one context object through their business logic.Backward compatibility
requestContext.getConnectionId()continues to work.Error handling / fallback
getConnectionContext()returnsnullwhenrequestContextorconnectionIdis missing.getManagementApiEndpoint()returnsnullwhendomainNameorstageis missing/empty.